﻿using Ducu.GLB.Global;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ducu.BLL.Entity
{
    public class HistoryItem : BaseEntity
    {
        public virtual Task Task { get; set; }
        public virtual Status Status { get; set; }
        public virtual string MyComment { get; set; }
        public virtual User Executor { get; set; }

        public virtual void Build()
        {
            Task.Add(this);
        }

        public virtual void Leave()
        {
            Task.Add(this);
        }

        public virtual void Accept()
        {
            Task.Add(this);
            sendMessage(Task.Plan.Vower);
        }

        public virtual void Reply()
        {
            Task.Add(this);
            if (Status == Status.Empty)
            {
                sendMessage(Task.GetPreviousChatter());
            }
        }

        private void sendMessage(User sendTo)
        {
            Message message = new Message
            {
                SendTo = sendTo,
                Reply = TaskTemplate.OnHistory(Task.Id.ToString())
            };

            string action = null;
            switch (Status)
            {
                case Status.Accepted:
                    action = AcceptTemplate.Accept;
                    break;
                case Status.Refused:
                    action = AcceptTemplate.Refuse;
                    break;
                case Status.Question:
                    action = AcceptTemplate.Question;
                    break;
                case Status.Empty:
                    action = AcceptTemplate.Reply;
                    break;
                default:
                    //TODO: 
                    throw new Exception("not");
            }

            message.Content = AcceptTemplate.MakeLink(
                Executor.Id.ToString(), Executor.Profile.DisplayName,
                action,
                Task.Plan.Id.ToString(), Task.Plan.Title,
                MyComment);

            sendTo.AddMessage(message);        
        }

        public virtual Status GetStatus()
        {
            return Status;
        }
    }
}
